package com.iwhalecloud.bss.kite.cucc.service.attr.handler;

import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.service.attr.LocalAttrHelper;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteProdInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.iwhalecloud.bss.kite.service.attr.handler.AbstractAttrHandler;
import org.springframework.beans.factory.annotation.Autowired;


import java.util.List;
import java.util.Objects;

/**
 * 数盾风控付费方式和付费类型联动
 */
public class PayTypeAndWayFKAttrHandler  extends AbstractAttrHandler<KiteInst, AttrData> {
    @Autowired
    private IQuerySceneInstService querySceneInstService;
    @Override
    public void set(KiteAttr attr, KiteInst inst, String value, AttrData data) {
        super.set(attr, inst, value, data);
        String fieldName = attr.getFieldName();
        //1、付费类型为1-预付费：扣费方式字段展示并必填；
        //2、付费类型为2-后付费：不展示该属性
        if(KiteStringUtils.equals(fieldName,"200001858")){
            String payType = attr.getValue();
            if(KiteStringUtils.equals(payType,"100007577")){
                LocalAttrHelper.showAndNotNull(inst, "200004285");
            }else{
                LocalAttrHelper.hideAndNullable(inst,"200004285", "200004288", "210005386");
            }
        }else if(KiteStringUtils.equals(fieldName,"200004285")){
            String payWay = attr.getValue();
            if(KiteStringUtils.equals(payWay,"100007579")){
                LocalAttrHelper.showAndNotNull(inst, "200004288");
                LocalAttrHelper.hideAndNullable(inst, "210005386");
            }else if(KiteStringUtils.equalsAny(payWay,"100007580","100007581")){
                LocalAttrHelper.showAndNotNull(inst, "210005386");
                LocalAttrHelper.hideAndNullable(inst, "200004288");
            }else {
                LocalAttrHelper.hideAndNullable(inst, "200004288","210005386");
            }
            //当选择“2-单独按次计费”，每个附加商品的规格上需要联动展示属性：200001053（调用次数）
            String sceneInstId = data.getSceneInstId();
            //获取所有产品实例
            List<KiteProdInst> kiteProdInsts = querySceneInstService.listProdInsts(sceneInstId);
            if(KiteListUtils.isNotEmpty(kiteProdInsts) && kiteProdInsts.size()>1){
                for(KiteProdInst prodInst:kiteProdInsts){
                    KiteAttr attr_200001053 = prodInst.getAttr("200001053");
                    if(Objects.nonNull(attr_200001053)){
                        if(KiteStringUtils.equals(payWay,"100007580")) {
                            LocalAttrHelper.hiddenOrDisplayProdAttr(data.getSceneInstId(), prodInst, attr_200001053, KeyConsts.PROD_ATTR_SHOW_TYPE_D, attr_200001053.getValue());
                            //LocalAttrHelper.showForDAndNull(inst, "200001053");
                        }else {
                            LocalAttrHelper.hiddenOrDisplayProdAttr(data.getSceneInstId(), prodInst, attr_200001053, KeyConsts.PROD_ATTR_SHOW_TYPE_E, attr_200001053.getValue());
                            //LocalAttrHelper.hideAndNullable(prodInst, "200001053");
                        }
                    }
                }
            }
        }
    }

    @Override
    public void after(KiteAttr attr, KiteInst inst, AttrData data) {
        String fieldName = attr.getFieldName();
        if(KiteStringUtils.equals(fieldName,"200001053")){
            //获取所有产品实例
            List<KiteProdInst> kiteProdInsts = querySceneInstService.listProdInsts(data.getSceneInstId());
            if(KiteListUtils.isNotEmpty(kiteProdInsts)){
                boolean isShow = kiteProdInsts.stream().anyMatch(prodInst->{
                    KiteAttr attr_200004285 = prodInst.getAttr("200004285");
                    return Objects.nonNull(attr_200004285)&&KiteStringUtils.equals(attr_200004285.getShowType(),KeyConsts.PROD_ATTR_SHOW_TYPE_D)&&KiteStringUtils.equals(attr_200004285.getValue(),"100007580");
                });
                if(isShow){
                    LocalAttrHelper.showForDAndNull(inst, "200001053");
                }else {
                    LocalAttrHelper.hideAndNullable(inst, "200001053");
                }
            }
            super.after(attr, inst, data);
        }
    }
}
